/*
 * Filename:    InsuredMedicalDetailController.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-1-28
 */
package com.paic.mhis.hcpms.trust.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.hcpms.trust.biz.service.InsuredMedicalDetailService;
import com.paic.mhis.hcpms.trust.dto.IntegrityMedicalDTO;
import com.paic.mhis.hcpms.trust.web.vo.IntegrityMedicalVO;
import com.paic.mhis.hcpms.trust.web.vo.TrustUtil;
import com.paic.pafa.app.biz.service.BusinessServiceException;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.exceptions.BusinessException;

@Controller
@RequestMapping("/integrityDetail/")
public class InsuredMedicalDetailController {
	@BizServicesProxy(beanName = "insuredMedicalDetailService")
	private InsuredMedicalDetailService insuredMedicalDetailService;

	/**
	 * 医务人员详情查询列表
	 * 
	 * @param limit
	 * @param start
	 * @param search
	 * @param request
	 * @return 返回查询的集合和总条数
	 * @throws BusinessServiceException
	 * @throws BusinessException
	 */
	@RequestMapping("integrityDetailList.do")
	@ResponseBody
	public Map<String, Object> integrityDetailList(@ModelAttribute IntegrityMedicalVO search,
			@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "start", defaultValue = "0") int start, HttpServletRequest request)
			throws BusinessServiceException, BusinessException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("limit", limit);
		paramMap.put("start", start);
		TrustUtil.paramMap(paramMap, search);
		try {
			result = insuredMedicalDetailService.getMedicalDetailList(paramMap);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医务人员详情查询导出
	 * 
	 * @param limit
	 * @param start
	 * @param excelName
	 * @param vo
	 * @param response
	 */
	@RequestMapping("medicalDetailReport.do")
	@ResponseBody
	public void medicalDetailReport(@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "start", defaultValue = "0") int start,
			@RequestParam(value = "excelName", defaultValue = "medicalDetail") String excelName,
			@ModelAttribute IntegrityMedicalVO vo, HttpServletResponse response) {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		TrustUtil.paramMap(paramMap, vo);
		int total = insuredMedicalDetailService.getCountMedicalDetailReport(paramMap);
		List<IntegrityMedicalDTO> list = new ArrayList<IntegrityMedicalDTO>();
		if (total > 0) {
			limit = 8000;
			for (int i = 0; i < total;) {
				List<IntegrityMedicalDTO> resultListTemp = insuredMedicalDetailService.findMedicalDetailReport(limit,
						limit + 1, paramMap);
				list.addAll(resultListTemp);
				i += limit;
			}
		} else {
			list = new ArrayList<IntegrityMedicalDTO>();
		}
		String[] str = { "序号", "医生姓名", "身份证号", "机构名称", "机构编号", "机构地址", "执业时间", "违规记分值", "案件编号", "记分原因", "案发时间",
				"暂停医保服务权限时间" };
		try {
			exportExcel(str, excelName, list, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void exportExcel(String[] str, String excelName, List<IntegrityMedicalDTO> list,
			HttpServletResponse response) throws IOException {
		Workbook excel = null;
		try {
			excel = new HSSFWorkbook();
			Sheet sheet = excel.createSheet(excelName);
			Row row = sheet.createRow(0);
			// 设置Excel头
			int j = 0;
			for (String key : str) {
				row.createCell(j).setCellValue(key);
				sheet.setColumnWidth(j, 4000);
				j++;
			}
			// 设置Excel内容
			if (null != list) {
				for (int i = 0; i < list.size(); i++) {
					IntegrityMedicalDTO dto = list.get(i);
					row = sheet.createRow(i + 1);
					// 序号、医生姓名、身份证号、机构名称、机构编号、机构地址,执业时间、违规记分值、案件编号、记分原因、案发时间、暂停医保服务权限时间
					row.createCell(0).setCellValue(i + 1);// 序号
					row.createCell(1).setCellValue(dto.getName());// 医生姓名
					row.createCell(2).setCellValue(dto.getCertificateNo());// 身份证号
					row.createCell(3).setCellValue(dto.getHealthcareProviderName());// 机构名称
					row.createCell(4).setCellValue(dto.getHealthcareProviderCode());// 机构编号
					row.createCell(5).setCellValue(dto.getFullAddress());// 机构地址
					row.createCell(6).setCellValue(dto.getQualifiedDate());// 执业时间
					//row.createCell(7).setCellValue(dto.getViolationsScores());// 违规记分值
					row.createCell(7).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
					if(dto.getViolationsScores()!=null&&dto.getViolationsScores()!=""){
						row.getCell(7).setCellValue(Double.parseDouble(dto.getViolationsScores()));
					}
					else{
						row.getCell(7).setCellValue(Double.parseDouble("0.0"));
					}
					row.createCell(8).setCellValue(dto.getCaseNo());// 案件编号
					row.createCell(9).setCellValue(dto.getReason());// 记分原因
					row.createCell(10).setCellValue(dto.getHappenDate());// 案发时间
					row.createCell(11).setCellValue(dto.getSuspendPayExecutionTime());// 暂停医保服务权限时间
				}
			}
			initResponse(response, excelName);
			excel.write(response.getOutputStream());
		} catch (IOException e) {
			// logger.error("导出Excel是发生异常,异常原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			response.getOutputStream().flush();
			response.getOutputStream().close();
		}

	}

	private void initResponse(HttpServletResponse response, String excelName) {
		try {
			response.addHeader("Pragma", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
			response.reset();
			response.setContentType("APPLICATION/OCTET-STREAM");
			String fileName = new String((excelName).getBytes("utf-8"), "ISO8859-1") + ".xls";
			response.setHeader("content-disposition", "attachment;fileName=\"" + fileName + "\"");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
}
